package net.googlecode.demenkov.dao.impl;

import net.googlecode.demenkov.dao.FriendshipDAO;
import net.googlecode.demenkov.domains.Friendship;
import net.googlecode.demenkov.domains.Person;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;

/**
 * DAO-class for actions with friendships
 *
 * @author Demenkov Yura
 */
@Repository
public class FriendshipDAOImpl extends GenericDAOImpl<Friendship, Integer> implements FriendshipDAO {
    public Friendship findFriendshipByPersons(Person personWhoSend, Person personWhoGet) {
        Query query = entityManager.createQuery("from Friendship where personWhoProposedFriendship=:send " +
                "and personWhoGotFriendshipPropose=:got");
        query.setParameter("send", personWhoSend);
        query.setParameter("got", personWhoGet);
        if (!query.getResultList().isEmpty()) {
            return (Friendship) query.getResultList().get(0);
        } else {
            query.setParameter("send", personWhoGet);
            query.setParameter("got", personWhoSend);
            if (!query.getResultList().isEmpty()) {
                return (Friendship) query.getResultList().get(0);
            } else {
                return null;
            }
        }
    }

}
